package com.me.springcloud.webflux;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import lombok.Data;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

//webflux test 222
public class FlowTest {
	public static void main(String[] args) {
		new FlowTest().findUserById2("xxx").subscribe(System.out::println);
		new FlowTest().findUserById("aa").filter(t -> "aa".equals(t.getName())).subscribe(System.out::println);
		new FlowTest().findUserList().subscribe(System.out::println);
	}

	public Mono<User> findUserById2(String userId) {
		try {
			TimeUnit.SECONDS.sleep(5);
		} catch (Exception e) {
		}
		return findUserById(userId);
	}

	public Mono<User> findUserById(String userId) {
		User user = new User();
		user.setName(userId);
		// user = findUserById
		return Mono.just(user);
	}

	public Flux<User> findUserList() {
		User user = new User();
		user.setName("a");

		User user2 = new User();
		user2.setName("b");

		List<User> userList = new ArrayList<>();
		userList.add(user);
		userList.add(user2);
		return Flux.fromIterable(userList);
		// return Flux.fromStream(userList.stream());
	}

	@Data
	static class User {
		private String name;
	}
}